const mysql = require('./mysql');

let insertEntries = async function (name, discribe, image,interest_id,userId) {
    let sql = `insert into topic(name, discribe,image,interest_id,author_user_id) values('${name}','${discribe}','${image}','${interest_id}',${userId});`;
    return await mysql.asyncExec(sql);
}

let getAllEntries = async function () {
    let sql = `select id,name,image from topic;`;
    return await mysql.asyncExec(sql);
}

let getBetweenEntries = async function (first, last, interestId) {
    let sql = '';
    if (interestId == 0) {
        sql = `select id,name,image,subscripe_num from topic order by id desc limit ${first},${last};`;
    } else {
        sql = `select id,name,image,subscripe_num from topic where interest_id = ${interestId} order by id desc limit ${first},${last};`;
    }
    return await mysql.asyncExec(sql);
}

let getTopicByUserId = async function(userId){
    let sql  = `select * from topic where author_user_id = ${userId} order by id desc;`
    return await mysql.asyncExec(sql);
}

let getEntriesCount = async function (interestId) {
    let sql = '';
    if (interestId == 0) {
        sql = `select count(*) from topic`;
    } else {
        sql = `select count(*) from topic where interest_id = ${interestId}`;
    }
    let result = await mysql.asyncExec(sql);
    //直接返回数字结果
    return result[0]['count(*)'];
}

let getEntriesDataById = async function (topicId) {
    let sql = `select * from topic where id = ${topicId};`;
    let result = await mysql.asyncExec(sql);
    //没找到返回null
    return result === [] ? null : result[0];
}


let addSubscribeNumber = async function (topicId) {
    let sql = `update topic set subscripe_num = subscripe_num + 1 where id = ${topicId};`;
    await mysql.asyncExec(sql);
}

let reduceSubscibeNumber = async function (topicId) {
    let sql = `update topic set subscripe_num = subscripe_num - 1 where id = ${topicId} and subscripe_num > 0;`;
    await mysql.asyncExec(sql);
}

const MAXpathEntries = 9;

let getSplitTobic = async function (queryPath, interestId) {
    //topic条目总数
    let topicNum = await getEntriesCount(interestId);
    //path整数
    let pathIntNum = parseInt(topicNum / MAXpathEntries);
    //path余数
    let pathMod = pathIntNum % MAXpathEntries;
    //path总数，如果有余数就+1
    let pathNum = pathIntNum + (pathMod != 0 ? 1 : 0);

    //query的值，大于总页数就让他等于总页数 避免超越
    if (queryPath > pathNum && pathNum > 0) {
        queryPath = pathNum;
    }

    //当前的页码
    let currentPathNum = queryPath;
    //剩余的path数
    let exitsToicNum = pathNum - currentPathNum + 1;

    //到数据库获取值
    let last = currentPathNum * MAXpathEntries;
    let first = last - MAXpathEntries;

    let topic = await getBetweenEntries(first, MAXpathEntries, interestId);

    return {
        pathNum,        //path总数，如果有余数就+1
        exitsToicNum,   //剩余的path数
        currentPathNum, //当前的页码
        topic           //内容主体
    }
}



module.exports = {
    insertEntries,
    getAllEntries,
    getEntriesCount,
    getBetweenEntries,
    getEntriesDataById,
    addSubscribeNumber,
    reduceSubscibeNumber,
    getSplitTobic,
    getTopicByUserId
};